﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MxTol.Managers.Helpers;
using System.Data;

namespace MxTol.Managers
{
    /// <summary>
    /// 财经法规案例分析题资料文字描述管理器
    /// </summary>
    public class FrAnalysisDescripeManager
    {
        /// <summary>
        /// 以Json分页格式返回所有资料描述
        /// </summary>
        public static string GetAllDescripe(int currentPage, int pageSize)
        {
            string json = string.Empty;

            try
            {
                string sql = "select * from Fr_Analysis_Descripe order by add_time desc;";
                json = PaginationHelper.GetJsonBySql(currentPage, pageSize, sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return json;
        }

        /// <summary>
        /// 以Json分页格式返回所有资料描述
        /// </summary>
        public static string GetAllDescripe(int currentPage, int pageSize, string sql)
        {
            string json = string.Empty;

            try
            {
                json = PaginationHelper.GetJsonBySql(currentPage, pageSize, sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return json;
        }

        /// <summary>
        /// 获取财经法规案例分析题
        /// 注：返回2题，可随机返回2条记录或按照时间排序TOP 2
        /// </summary>
        public static List<Dictionary<string, object>> GetDescripe(bool isNew = false)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            try
            {
                // 选出2条案例资料文本
                // 随机20条
                string sql = @"select top 2 id,descripe,NEWID() as random from Fr_Analysis_Descripe order by random;";
                if (isNew)
                {
                    // 最新20条
                    sql = @"select top 2 id,descripe from Fr_Analysis_Descripe order by add_time desc;";
                }

                DataSet descripeSet = SqlHelper.ExecuteDataSet(ManagerConfig.ConnectionString, sql);
                if (descripeSet != null && descripeSet.Tables.Count > 0)
                {
                    DataTable descripeTable = descripeSet.Tables[0];
                    for (int i = 0; i < descripeTable.Rows.Count; i++)
                    {
                        string id = descripeTable.Rows[i][0].ToString();
                        string text = descripeTable.Rows[i][1].ToString();

                        Dictionary<string, object> choiceDic = new Dictionary<string, object>()
                        {
                            {"id",id},
                            {"title",text},
                            {"choice",FrAnalysisChoiceManager.GetChoiceByDescripeID(id)}
                        };

                        list.Add(choiceDic);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
